#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <filesystem>
#include <map>

namespace fs = std::filesystem;

class BuildManager {
public:
    BuildManager() {
        // 初始化工具链信息
        toolchains_ = {
            {"vs2022", {
                "Visual Studio 2022 Enterprise",
                "C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Enterprise\\\\Common7\\\\Tools\\\\VsDevCmd.bat",
                "build-vs2022.bat"
            }},
            {"msys2", {
                "MSYS2 UCRT64",
                "",
                "build-msys2.sh"
            }}
        };
        
        project_root_ = "D:/vc/devops-platform";
        scripts_dir_ = project_root_ + "/tools/scripts";
    }
    
    void detectToolchains() {
        std::cout << "=== 检测可用工具链 ===\n\n";
        
        for (const auto& [name, info] : toolchains_) {
            std::cout << "🔧 " << info.name << ":\n";
            
            if (name == "vs2022") {
                if (fs::exists(info.path)) {
                    std::cout << "   ✅ 已安装\n";
                    std::cout << "   路径: " << info.path << "\n";
                } else {
                    std::cout << "   ❌ 未找到\n";
                }
            } else if (name == "msys2") {
                // 检查 MSYS2 环境
                if (std::system("which gcc > /dev/null 2>&1") == 0) {
                    std::cout << "   ✅ GCC 可用: ";
                    std::system("gcc --version | head -1");
                } else {
                    std::cout << "   ❌ GCC 未找到\n";
                }
            }
            std::cout << "\n";
        }
        
        // 检查其他工具
        std::cout << "其他工具:\n";
        checkTool("CMake", "cmake --version");
        checkTool("Git", "git --version");
        checkTool("SQLite3", "sqlite3 --version");
    }
    
    bool buildWithVS2022() {
        std::cout << "=== 使用 Visual Studio 2022 构建 ===\n";
        
        if (!fs::exists(toolchains_["vs2022"].path)) {
            std::cout << "❌ VS2022 未安装\n";
            return false;
        }
        
        std::string build_script = scripts_dir_ + "/" + toolchains_["vs2022"].script;
        if (!fs::exists(build_script)) {
            std::cout << "❌ 构建脚本不存在: " << build_script << "\n";
            return false;
        }
        
        std::string command = "cmd.exe /c \"" + build_script + "\"";
        std::cout << "执行: " << command << "\n";
        
        int result = std::system(command.c_str());
        return result == 0;
    }
    
    bool buildWithMSYS2() {
        std::cout << "=== 使用 MSYS2 MinGW 构建 ===\n";
        
        std::string build_script = scripts_dir_ + "/" + toolchains_["msys2"].script;
        if (!fs::exists(build_script)) {
            std::cout << "❌ 构建脚本不存在: " << build_script << "\n";
            return false;
        }
        
        std::string command = "\"" + build_script + "\"";
        std::cout << "执行: " << command << "\n";
        
        int result = std::system(command.c_str());
        return result == 0;
    }
    
    void showUsage() {
        std::cout << "🚀 DevOps Platform 构建管理器 (C++ 版本)\n\n";
        std::cout << "用法: build-manager [command]\n\n";
        std::cout << "命令:\n";
        std::cout << "  detect   - 检测可用工具链\n";
        std::cout << "  vs2022   - 使用 Visual Studio 2022 构建\n";
        std::cout << "  msys2    - 使用 MSYS2 MinGW 构建\n";
        std::cout << "  help     - 显示此帮助信息\n\n";
        std::cout << "💡 推荐: build-manager vs2022 (性能最佳)\n";
        std::cout << "💡 备选: build-manager msys2 (跨平台兼容)\n";
    }

private:
    struct ToolchainInfo {
        std::string name;
        std::string path;
        std::string script;
    };
    
    std::map<std::string, ToolchainInfo> toolchains_;
    std::string project_root_;
    std::string scripts_dir_;
    
    void checkTool(const std::string& name, const std::string& check_cmd) {
        std::cout << "   " << name << ": ";
        if (std::system((check_cmd + " > /dev/null 2>&1").c_str()) == 0) {
            std::cout << "✅ 可用\n";
        } else {
            std::cout << "❌ 未找到\n";
        }
    }
};

int main(int argc, char* argv[]) {
    BuildManager manager;
    
    if (argc < 2) {
        manager.showUsage();
        return 1;
    }
    
    std::string command = argv[1];
    
    if (command == "detect") {
        manager.detectToolchains();
    } else if (command == "vs2022") {
        return manager.buildWithVS2022() ? 0 : 1;
    } else if (command == "msys2") {
        return manager.buildWithMSYS2() ? 0 : 1;
    } else if (command == "help") {
        manager.showUsage();
    } else {
        std::cout << "❌ 未知命令: " << command << "\n\n";
        manager.showUsage();
        return 1;
    }
    
    return 0;
}
